<?php

namespace frontend\frontBundle\Controller\Admin;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use mylibs\common\Paginator;

 /**
  * 
  *Class for login, regist user
 * Author: DucLH
 */
class AdproductController extends Controller{
/**
    * 
    * Show list Product
    * Author: DucLH
    */
  public function indexAction() {
  	/*	
  	if ($this->get('security.context')->isGranted('ROLE_ADMIN')) {
       	
    }
	$user = $this->get('security.context')->getToken()->getUser();
	*/
  	$iPage = $this->getRequest()->get('page',1);
  	$em      	= $this->container->get('doctrine')->getEntityManager();
  	
    $aNews	 	= $em->getRepository('frontendfrontBundle:News')->getNews(ROWPERPAGE,($iPage-1)*ROWPERPAGE);
    $iTotalNews = count($aNews);
    $oPaginator = new Paginator($iTotalNews,$this->getRequest()->get('page',1),ROWPERPAGE);
    $templating = $this->get('templating');
    $paging = $templating->render('::paginator.html.php', array('paginator' => $oPaginator, 'controller' => $this->container->get('request')->get('_route')));   
     
    return $this->render('frontendfrontBundle:Admin/Product:index.html.php',array("aNews"=>$aNews,"paging"=> $paging));
  }
  /**
    * 
    * Add new Product
    * Author: DucLH
    */
 public function addAction() {
  	/*	
  	if ($this->get('security.context')->isGranted('ROLE_ADMIN')) {
       	
    }
	$user = $this->get('security.context')->getToken()->getUser();
	*/
 	$sRootWeb = $this->get('kernel')->getRootDir() . '/../web/uploads/product/';
 	
 	$aMessageMap = unserialize(MESSAGE_MAP);
 	$aErr = array();
 	$data = array();
 	$request = $this->get('request');
 	if ($request->getMethod() == 'POST') {
    	//Get ALL Data post
    	$data = $request->request->all();
    	if($data['txtTitle'] == ""){
    		$aErr['txtTitle'] = "PRODUCT_ADD_ERR_0001";
    	}
    	if($data['txtDescription'] == ""){
    		$aErr['txtDescription'] = "PRODUCT_ADD_ERR_0002";
    	}
    	if($data['editor1'] == ""){
    		$aErr['editor1'] = "PRODUCT_ADD_ERR_0003";
    	}
    	//Get img upload
    	$uploadFile = $request->files->get("fImge");
        if($uploadFile == NULL) {
         	$aErr['fImge'] = "PRODUCT_ADD_ERR_0004";
        }else{
	        $aFileType = array("jpeg","png");
	        if(iSset($aErr['fImge']) && $aErr['fImge'] == ""){
	        	$originalFileName = $uploadFile->getClientOriginalName();
	        }elseif(!in_array($uploadFile->guessExtension(), $aFileType)){
	        	$aErr['fImge'] = "PRODUCT_ADD_ERR_0005";
	        }
        }
        if(!count($aErr)>0){//Case have err
        	$data['Imgpath'] = date("Ymdhis").".".$uploadFile->guessExtension();
        	//var_dump("<pre>",$aErr,"co loi xay ra");
        	$em      	= $this->container->get('doctrine')->getEntityManager();
        	$iNewId 	= $em->getRepository('frontendfrontBundle:News')->insertNew($data);
        	if($iNewId > 0)
        		$newFile = $uploadFile->move($sRootWeb, $data['Imgpath']);
        	return $this->redirect($this->generateUrl('frontendfront_admin_Product'));	
        }
    	
    }
  	return $this->render('frontendfrontBundle:Admin/Product:add.html.php',array("aErr"=>$aErr,"aMessageMap"=>$aMessageMap,"aData" => $data));
  }
  /**
    * 
    * Detail new Product
    * Author: DucLH
    */
	 public function detailAction() {
	 	$request = $this->get('request');
	 	$id = $request->query->get('id');
	 	$em      	= $this->container->get('doctrine')->getEntityManager();
        $oNew 	= $em->getRepository('frontendfrontBundle:News')->getNewsById($id);
        //var_dump($oNew);
	 	return $this->render('frontendfrontBundle:Admin/Product:detail.html.php',array("oNew"=>$oNew));
	 }
/**
    * 
    * Detail new Product
    * Author: DucLH
    */
	 public function editAction() {
	 	$request = $this->get('request');
	 	$id = $request->query->get('id');
	 	$em      	= $this->container->get('doctrine')->getEntityManager();
        $oNew 	= $em->getRepository('frontendfrontBundle:News')->getNewsById($id);
        return $this->render('frontendfrontBundle:Admin/Product:edit.html.php',array("oNew"=>$oNew));
	 }
}